﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Web.Security;
using System.Text.RegularExpressions;

namespace IndyMosaic.Core.Email
{
    public class EmailUtility
    {
        public static bool OverrideDelivery
        {
            get
            {
                string overrideValue = ConfigurationManager.AppSettings["Email.Override"];
                if (string.IsNullOrEmpty(overrideValue))
                {
                    return false;
                }
                else
                {
                    return bool.Parse(overrideValue);
                }
            }
        }

        public static string OverrideAddress
        {
            get
            {
                return ConfigurationManager.AppSettings["Email.OverrideAddress"];
            }
        }
        public static MailAddress NotificationsAddress
        {
            get
            {
                string address = ConfigurationManager.AppSettings["Email.NotificationsAddress"];
                return new MailAddress(address);
            }
        }
        public static MailAddress PrayerRequestAddressCommunity
        {
            get
            {
                string address = ConfigurationManager.AppSettings["Email.PrayerRequestAddressCommunity"];
                return new MailAddress(address);
            }
        }
        public static MailAddress PrayerRequestAddressConfidential
        {
            get
            {
                string address = ConfigurationManager.AppSettings["Email.PrayerRequestAddressConfidential"];
                return new MailAddress(address);
            }
        }
        public static MailAddress PrayerRequestAddressLeaders
        {
            get
            {
                string address = ConfigurationManager.AppSettings["Email.PrayerRequestAddressLeaders"];
                return new MailAddress(address);
            }
        }

        public static MailAddress GetMailAddress(MembershipUser user, string displayName)
        {
            return new MailAddress(user.Email, displayName);
        }
        public static void SendEmail(MailAddress fromAddress, MailAddress toAddress, string subject, string body)
        {
            MailAddressCollection toAddresses = new MailAddressCollection();
            toAddresses.Add(toAddress);
            SendEmail(fromAddress, toAddresses, subject, body);
        }
        public static void SendEmail(MailAddress fromAddress, MailAddressCollection toAddresses, string subject, string body)
        {
            MailMessage message = new MailMessage();
            message.Subject = subject;
            message.From = fromAddress;
            message.Body = body;
            message.IsBodyHtml = true;

            AddToRecipients(toAddresses, message);

            SmtpClient client = new SmtpClient();
            
            client.Send(message);
        }
        
        private static void AddToRecipients(MailAddressCollection toAddresses, MailMessage message)
        {
            if (OverrideDelivery)
            {
                message.To.Add(OverrideAddress);
                InsertOverrideIntoSubject(toAddresses, message);
            }
            else
            {
                foreach (MailAddress toAddress in toAddresses)
                {
                    message.To.Add(toAddress);
                }
            }
        }

        private static void InsertOverrideIntoSubject(MailAddressCollection toAddresses, MailMessage message)
        {
            StringBuilder subject = new StringBuilder("Originally sent to:");
            foreach (MailAddress toAddress in toAddresses)
            {
                subject.AppendFormat(" {0}<{1}>,", toAddress.DisplayName, toAddress.Address);
            }
            subject.Append(message.Subject);
            message.Subject = subject.ToString();
        }

    }
}
